﻿using System;
using System.Windows.Forms;

namespace Связь_с_БД
{
    public partial class FormUpdateRow : Form
    {
        public FormUpdateRow()
        {
            InitializeComponent();
            DocumentStudentValue = new string[10];
            ExamsValue = new string[9];
            JobsValue = new string[6];
            LearningDataValue = new string[13];
            LearningValue = new string[9];
            LearningDataDocumentValue = new string[8];
            ParentsValue = new string[4];
            PaymentsValue = new string[3];
            PreviousEducationValue = new string[9];
            PrivateDataValue = new string[16];
        }

        private SplitEntryForUpdate _entry;
        public string[] DocumentStudentValue;
        public string[] ExamsValue;
        public string[] JobsValue;
        public string[] LearningDataValue;
        public string[] LearningValue;
        public string[] LearningDataDocumentValue;
        public string[] ParentsValue;
        public string[] PaymentsValue;
        public string[] PreviousEducationValue;
        public string[] PrivateDataValue;
        private int _id;
        private const string DateEmptyShort = @"  .  .";
        private const string DateEmptyLong = @"  .  .       :";
        private const string DateShort = @"1950-01-01";
        private const string DateLong = @"1950-01-01 0:00:00";

        public void SetGridRow(DataGridViewRow dgvr)
        {
            if (dgvr != null)
            {
                SetId(dgvr);
                _entry = new SplitEntryForUpdate(dgvr);
            }
        }

        public void GetOldValue()
        {
            DocumentStudentValue = _entry.GetDocumentStudentValue();
            ExamsValue = _entry.GetExamsValue();
            JobsValue = _entry.GetJobsValue();
            LearningDataValue = _entry.GetLearningDataValue();
            LearningValue = _entry.GetLearningValue();
            LearningDataDocumentValue = _entry.GetLearningDataDocumentValue();
            ParentsValue = _entry.GetParentsValue();
            PaymentsValue = _entry.GetPaymentsValue();
            PreviousEducationValue = _entry.GetPreviousEducationValue();
            PrivateDataValue = _entry.GetPrivateDataValue();
        }

        public void SetOldValueOnForm()
        {
            SetOldDocumentStudent();
            SetOldExams();
            SetOldJobs();
            SetOldLearning();
            SetOldLearningData();
            SetOldLearningDataDocument();
            SetOldParents();
            SetOldPayments();
            SetOldPreviousEducation();
            SetOldPrivateData();
        }

        #region Вывод старых значений
        private void SetOldDocumentStudent()
        {
            textBox112.Text = DocumentStudentValue[0];
            textBox111.Text = DocumentStudentValue[1];
            textBox110.Text = DocumentStudentValue[2];
            textBox109.Text = DocumentStudentValue[3];
            textBox107.Text = DocumentStudentValue[4];
            textBox108.Text = DocumentStudentValue[5];
            textBox106.Text = DocumentStudentValue[6];
            textBox105.Text = DocumentStudentValue[7];
            textBox104.Text = DocumentStudentValue[8];
            textBox102.Text = DocumentStudentValue[9];
        }
        private void SetOldExams()
        {
            textBox132.Text = ExamsValue[0];
            textBox130.Text = ExamsValue[1];
            textBox131.Text = ExamsValue[2];
            textBox129.Text = ExamsValue[3];
            textBox128.Text = ExamsValue[4];
            textBox127.Text = ExamsValue[5];
            textBox125.Text = ExamsValue[6];
            textBox124.Text = ExamsValue[7];
            textBox123.Text = ExamsValue[8];
        }
        private void SetOldJobs()
        {
            textBox12.Text = JobsValue[0];
            textBox103.Text = JobsValue[1];
            textBox116.Text = JobsValue[2];
            textBox115.Text = JobsValue[3];
            textBox114.Text = JobsValue[4];
            textBox113.Text = JobsValue[5];
        }
        private void SetOldLearningData()
        {
            textBox32.Text = LearningDataValue[0];
            textBox31.Text = LearningDataValue[1];
            textBox30.Text = LearningDataValue[2];
            textBox29.Text = LearningDataValue[3];
            textBox28.Text = LearningDataValue[4];
            textBox27.Text = LearningDataValue[5];
            textBox26.Text = LearningDataValue[6];
            textBox25.Text = LearningDataValue[7];
            textBox24.Text = LearningDataValue[8];
            textBox23.Text = LearningDataValue[9];
            textBox22.Text = LearningDataValue[10];
            textBox21.Text = LearningDataValue[11];
            textBox20.Text = LearningDataValue[12];
        }
        private void SetOldLearning()
        {
            textBox126.Text = LearningValue[0];
            textBox139.Text = LearningValue[1];
            textBox138.Text = LearningValue[2];
            textBox137.Text = LearningValue[3];
            textBox136.Text = LearningValue[4];
            textBox135.Text = LearningValue[5];
            textBox134.Text = LearningValue[6];
            textBox133.Text = LearningValue[7];
            textBox117.Text = LearningValue[8];
        }
        private void SetOldLearningDataDocument()
        {
            maskedTextBox1.Text = LearningDataDocumentValue[0];
            textBox93.Text = LearningDataDocumentValue[1];
            textBox92.Text = LearningDataDocumentValue[2];
            textBox91.Text = LearningDataDocumentValue[3];
            textBox90.Text = LearningDataDocumentValue[4];
            textBox89.Text = LearningDataDocumentValue[5];
            textBox88.Text = LearningDataDocumentValue[6];
            textBox87.Text = LearningDataDocumentValue[7];
        }
        private void SetOldParents()
        {
            textBox119.Text = ParentsValue[0];
            textBox120.Text = ParentsValue[1];
            textBox121.Text = ParentsValue[2];
            textBox122.Text = ParentsValue[3];
        }
        private void SetOldPayments()
        {
            textBox19.Text = PaymentsValue[0];
            textBox18.Text = PaymentsValue[1];
            textBox17.Text = PaymentsValue[2];
        }
        private void SetOldPreviousEducation()
        {
            textBox101.Text = PreviousEducationValue[0];
            textBox100.Text = PreviousEducationValue[1];
            textBox99.Text = PreviousEducationValue[2];
            textBox98.Text = PreviousEducationValue[3];
            textBox97.Text = PreviousEducationValue[4];
            maskedTextBox2.Text = PreviousEducationValue[5];
            textBox96.Text = PreviousEducationValue[6];
            textBox95.Text = PreviousEducationValue[7];
            textBox94.Text = PreviousEducationValue[8];
        }
        private void SetOldPrivateData()
        {
            textBox34.Text = PrivateDataValue[0];
            textBox86.Text = PrivateDataValue[1];
            textBox85.Text = PrivateDataValue[2];
            textBox84.Text = PrivateDataValue[3];
            textBox83.Text = PrivateDataValue[4];
            textBox82.Text = PrivateDataValue[5];
            textBox79.Text = PrivateDataValue[6];
            textBox62.Text = PrivateDataValue[7];
            maskedTextBox3.Text = PrivateDataValue[8];
            textBox33.Text = PrivateDataValue[9];
            textBox60.Text = PrivateDataValue[10];
            textBox61.Text = PrivateDataValue[11];
            textBox37.Text = PrivateDataValue[12];
            textBox38.Text = PrivateDataValue[13];
            textBox36.Text = PrivateDataValue[14];
            textBox118.Text = PrivateDataValue[15];
        }
        #endregion 

        #region Получение новых значений
        private void SetDocumentStudentValue()
        {
            DocumentStudentValue[0] = textBox112.Text;
            DocumentStudentValue[1] = textBox111.Text;
            DocumentStudentValue[2] = textBox110.Text;
            DocumentStudentValue[3] = textBox109.Text;
            DocumentStudentValue[4] = textBox107.Text;
            DocumentStudentValue[5] = textBox108.Text;
            DocumentStudentValue[6] = textBox106.Text;
            DocumentStudentValue[7] = textBox105.Text;
            DocumentStudentValue[8] = textBox104.Text;
            DocumentStudentValue[9] = textBox102.Text;
        }
        private void SetExamsValue()
        {
            ExamsValue[0] = textBox132.Text;
            ExamsValue[1] = textBox130.Text;
            ExamsValue[2] = textBox131.Text;
            ExamsValue[3] = textBox129.Text;
            ExamsValue[4] = textBox128.Text;
            ExamsValue[5] = textBox127.Text;
            ExamsValue[6] = textBox125.Text;
            ExamsValue[7] = textBox124.Text;
            ExamsValue[8] = textBox123.Text;
        }
        private void SetJobsValue()
        {
            
            JobsValue[0] = textBox12.Text;
            JobsValue[1] = textBox103.Text;
            JobsValue[2] = textBox116.Text;
            JobsValue[3] = textBox115.Text;
            JobsValue[4] = textBox114.Text;
            JobsValue[5] = textBox113.Text;

        }
        private void SetLearningDataValue()
        {
            
            LearningDataValue[0] = textBox32.Text;
            LearningDataValue[1] = textBox31.Text;
            LearningDataValue[2] = textBox30.Text;
            LearningDataValue[3] = textBox29.Text;
            LearningDataValue[4] = textBox28.Text;
            LearningDataValue[5] = textBox27.Text;
            LearningDataValue[6] = textBox26.Text;
            LearningDataValue[7] = textBox25.Text;
            LearningDataValue[8] = textBox24.Text;
            LearningDataValue[9] = textBox23.Text;
            LearningDataValue[10] = textBox22.Text;
            LearningDataValue[11] = textBox21.Text;
            LearningDataValue[12] = textBox20.Text;
        }
        private void SetLearningValue()
        {
           
            LearningValue[0] = textBox126.Text;
            LearningValue[1] = textBox139.Text;
            LearningValue[2] = textBox138.Text;
            LearningValue[3] = textBox137.Text;
            LearningValue[4] = textBox136.Text;
            LearningValue[5] = textBox135.Text;
            LearningValue[6] = textBox134.Text;
            LearningValue[7] = textBox133.Text;
            LearningValue[8] = textBox117.Text;
        }
        private void SetLearningDataDocumentValue()
        {
            if (maskedTextBox1.Text == DateEmptyLong)
                LearningDataDocumentValue[0] = DateLong;
            else
            {
                string str = GetMySqlDateTime(GetYear(maskedTextBox1.Text),
                                                 GetMounth(maskedTextBox1.Text),
                                                           GetDay(maskedTextBox1.Text),
                                                                  GetHours(maskedTextBox1.Text),
                                                                           GetMinutes(maskedTextBox1.Text),
                                                                                      "00");
                LearningDataDocumentValue[0] = str;
            }
            LearningDataDocumentValue[1] = textBox93.Text;
            LearningDataDocumentValue[2] = textBox92.Text;
            LearningDataDocumentValue[3] = textBox91.Text;
            LearningDataDocumentValue[4] = textBox90.Text;
            LearningDataDocumentValue[5] = textBox89.Text;
            LearningDataDocumentValue[6] = textBox88.Text;
            LearningDataDocumentValue[7] = textBox87.Text;
        }
        private void SetParentsValue()
        {
            ParentsValue[0] = textBox119.Text;
            ParentsValue[1] = textBox120.Text;
            ParentsValue[2] = textBox121.Text;
            ParentsValue[3] = textBox122.Text;
        }
        private void SetPaymentsValue()
        {
            PaymentsValue[0] = textBox19.Text;
            PaymentsValue[1] = textBox18.Text;
            PaymentsValue[2] = textBox17.Text;
        }
        private void SetPreviousEducationValue()
        {
           
            PreviousEducationValue[0] = textBox101.Text;
            PreviousEducationValue[1] = textBox100.Text;
            PreviousEducationValue[2] = textBox99.Text;
            PreviousEducationValue[3] = textBox98.Text;
            PreviousEducationValue[4] = textBox97.Text;
            if (maskedTextBox2.Text == DateEmptyShort)
                PreviousEducationValue[5] = DateShort;
            else
            {
                string str = GetMySqlDate(GetYear(maskedTextBox2.Text),
                                      GetMounth(maskedTextBox2.Text),
                                      GetDay(maskedTextBox2.Text));
                PreviousEducationValue[5] = str;
            }
            PreviousEducationValue[5] = maskedTextBox2.Text;
            PreviousEducationValue[6] = textBox96.Text;
            PreviousEducationValue[7] = textBox95.Text;
            PreviousEducationValue[8] = textBox94.Text;
        }
        private void SetPrivateDataValue()
        {
           
            PrivateDataValue[0] = textBox34.Text;
            PrivateDataValue[1] = textBox86.Text;
            PrivateDataValue[2] = textBox85.Text;
            PrivateDataValue[3] = textBox84.Text;
            PrivateDataValue[4] = textBox83.Text;
            PrivateDataValue[5] = textBox82.Text;
            PrivateDataValue[6] = textBox79.Text;
            PrivateDataValue[7] = textBox62.Text;
            if (maskedTextBox3.Text == DateEmptyShort)
                PrivateDataValue[8] = DateShort;
            else
            {
                string str = GetMySqlDate(GetYear(maskedTextBox3.Text),
                                      GetMounth(maskedTextBox3.Text),
                                      GetDay(maskedTextBox3.Text));
                PrivateDataValue[8] = str;
            } 
            PrivateDataValue[9] = textBox33.Text;
            PrivateDataValue[10] = textBox60.Text;
            PrivateDataValue[11] = textBox61.Text;
            PrivateDataValue[12] = textBox37.Text;
            PrivateDataValue[13] = textBox38.Text;
            PrivateDataValue[14] = textBox36.Text;
            PrivateDataValue[15] = textBox118.Text;
        }
        #endregion

        public void SetAllData()
        {
            SetDocumentStudentValue();
            SetExamsValue();
            SetJobsValue();
            SetLearningDataDocumentValue();
            SetLearningDataValue();
            SetLearningValue();
            SetParentsValue();
            SetPaymentsValue();
            SetPreviousEducationValue();
            SetPrivateDataValue();
        }

        private void SetId(DataGridViewRow dgvr)
        {
             _id = Convert.ToInt32(dgvr.Cells[1].Value);
        }

        private void Button1Click(object sender, EventArgs e)
        {
            SetAllData();
            var f1 = Owner as FormMain;
            if (f1 != null)
            {
                f1.GetIdForUpdate(_id);
                f1.GetParametres(DocumentStudentValue, ExamsValue, JobsValue, LearningDataValue, LearningValue,
                                 LearningDataDocumentValue, ParentsValue, PaymentsValue, PreviousEducationValue,
                                 PrivateDataValue, "Update");
            }
            Close();
        }

        private void Button2Click(object sender, EventArgs e)
        {
            Close();
        }

        private string GetYear(string str)
        {
            string res = str[6].ToString() + str[7].ToString() + str[8].ToString() + str[9].ToString();
            return res;
        }
        private string GetMounth(string str)
        {
            string res = str[3].ToString() + str[4].ToString();
            return res;
        }
        private string GetDay(string str)
        {
            string res = str[0].ToString() + str[1].ToString();
            return res;
        }
        private string GetHours(string str)
        {
            string res = str[11].ToString() + str[12].ToString();
            return res;
        }
        private string GetMinutes(string str)
        {
            string res = str[14].ToString() + str[15].ToString();
            return res;
        }
        private string GetMySqlDateTime(string year, string mounth, string day, string hours, string minutes, string seconds)
        {
            string res = string.Empty;
            res += year;
            res += "-";
            res += mounth;
            res += "-";
            res += day;
            res += " ";
            res += hours;
            res += ":";
            res += minutes;
            res += ":";
            res += seconds;
            return res;
        }
        private string GetMySqlDate(string year, string mounth, string day)
        {
            string res = string.Empty;
            res += year;
            res += "-";
            res += mounth;
            res += "-";
            res += day;
            return res;
        }
    }
}
